import 'dart:convert' show json;
import '../utils/sp_utils.dart';
class UserModel {
  String userId;
  String loginname;
  String username;
  String password;
  String photoUrl;
  String loginTime;
  UserModel({this.userId,this.username,this.loginname,this.password, this.photoUrl, this.loginTime});
  String toJsonString() {
    Map map = {
      "username": username,
      "loginname": loginname,
      "password": password,
      "photoUrl": photoUrl,
      "loginTime": loginTime,
      "userId": userId,
    };
    return json.encode(map);
  }

  UserModel.fromJson(String jsonStr) {
    Map map = json.decode(jsonStr);
    this.username = map["username"];
    this.loginname = map["loginname"];
    this.password = map["password"];
    this.photoUrl = map["photoUrl"];
    this.loginTime = map["loginTime"];
    this.userId = map["userId"];
  }
}
class UserContext{
  static UserModel _user;
  static UserModel get user{
    if(_user==null) setCurrentUser();
    return _user;
  }
  static void clear()=>_user=null;
  static Future<void> setCurrentUser() async{
    SpUtils spUtils=new SpUtils();
    await spUtils.init();
    String userJson=spUtils.getString("USER");
    if(userJson!=null&&userJson.isNotEmpty) {
      _user = UserModel.fromJson(userJson);
    }
  }
}
